import { Action, createReducer, on } from '@ngrx/store';
import { Song } from 'src/app/services/data-types/common.types';
import { PlayMode } from 'src/app/share/wy-ui/wy-player/player-type';
import { SetCurrentAction, SetCurrentIndex, SetPlaying, SetPlayList, SetPlayMode, SetSongList } from '../actions/player.actions';

export enum CurrentActions{
  Add,
  Play,
  Delete,
  Clear,
  Other
}

export type PlayState = {
    // 播放状态
    playing: boolean;

    //播放模式
    playMode: PlayMode;

    // 歌曲列表
    songList: Song[];

    //播放列表
    playList: Song[];

    //当前正在播放的索引
    currentIndex: number;

    //当前操作
    currentAction: CurrentActions;
}


export const initialState: PlayState = {
    playing: false,
    songList: [],
    playList: [],
    playMode: {type:'loop',laber:"循环"},
    currentIndex: -1,
    currentAction: CurrentActions.Other
}

const reducer = createReducer(
    initialState,
    on(SetPlaying, (state:any, { playing }) => ({ ...state, playing })),
    on(SetPlayList, (state:any, { playList }) => ({ ...state, playList })),
    on(SetSongList, (state:any, { songList }) => ({ ...state, songList })),
    on(SetPlayMode, (state:any, { playMode }) => ({ ...state, playMode })),
    on(SetCurrentIndex, (state:any, { currentIndex }) => ({ ...state, currentIndex })),
    on(SetCurrentAction, (state:any, { currentAction }) => ({ ...state, currentAction })),
);

export function plpayerReducer(state:PlayState | undefined,action:Action){
    return reducer(state,action);
}


